//
//  CLTimeViewController.m
//  CareLink
//
//  Created by apple on 16/8/20.
//  Copyright © 2016年 Seven Team. All rights reserved.
//

#import "CLTimeViewController.h"
#import "CLTimeFlowLayout.h"
#import "CLTimeCollectionViewCell.h"
#import "CLBaseCollectionViewCell.h"
#import "CLSubmitDataVC.h"
static NSString* baseCellid = @"baseCellid";
static NSString* timeCellid = @"timeCellid";
static NSString* StarCellid = @"StarCellid";
@interface CLTimeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *testView;
@property (nonatomic,strong) UICollectionView *collectionview;
@property (nonatomic,strong) UICollectionViewFlowLayout *layout;
/**
 *  时间
 */
@property (nonatomic,copy) NSString *timeStr;


@end

@implementation CLTimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // 设置ui
    [self setupUI];
    // 上个页面传来的疾病信息
//    NSLog(@"%@", self.sickDataDict);
    // 方式一
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];

}
// 设置UI
- (void)setupUI
{
    // 添加事件表格(collectionview)
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    self.layout = layout;
    UICollectionView *collectionview = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionview.directionalLockEnabled = NO;
    collectionview.bounces = NO;
    collectionview.delegate = self;
    // 关闭滚动条
    collectionview.showsVerticalScrollIndicator = NO;
    collectionview.showsHorizontalScrollIndicator = NO;
    collectionview.bounces = NO;
    
    [self.view addSubview:collectionview];
    collectionview.backgroundColor = [UIColor colorWithHex:0xf5f5f5];
    
    // 注册单元格
    [collectionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:baseCellid];
    [collectionview registerNib:[UINib nibWithNibName:@"Date" bundle:nil]forCellWithReuseIdentifier:timeCellid];
    [collectionview registerNib:[UINib nibWithNibName:@"Star" bundle:nil]forCellWithReuseIdentifier:StarCellid];
  
    // 设置约束
    [collectionview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(self.testView.mas_bottom).offset(0);
        make.height.offset(250);
   
    }];
    // 设置数据源
    collectionview.dataSource = self;
    self.collectionview = collectionview;
}

#pragma mark -<UICollectionViewDataSource>


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 8;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *userid = baseCellid;
    // 加星,时间
    if(indexPath.section == 1 && indexPath.row == 3)
    {
        userid = StarCellid;
    }else if(indexPath.section == 2 && indexPath.row == 6)
    {
        userid = StarCellid;
    }else if(indexPath.section == 3 && indexPath.row == 4)
    {
        userid = StarCellid;
    }
    // 设置日期
    if (indexPath.section == 0 && indexPath.row > 0 && indexPath.row < 8 ) {
        userid = timeCellid;
        NSString *timeStr = [self getNDay:indexPath.row ];
        self.timeStr = timeStr;
    }
    CLBaseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:userid forIndexPath:indexPath];
    if (self.timeStr && indexPath.row < 8 && indexPath.section == 0) {
        // 日期label
        UILabel *dateLabel = [UILabel labelWithText:self.timeStr andTextColor:[UIColor lightGrayColor] andFontSize:13];
        [cell addSubview:dateLabel];
        [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(cell);
            make.bottom.offset(-8);
        }];
        // 星期几
        NSDate*nowDate = [NSDate date];
        
        NSDate* theDate;
        
        if(indexPath.row!= 0){
            
            NSTimeInterval  oneDay = 24*60*60*1;  //1天的长度
            theDate = [nowDate initWithTimeIntervalSinceNow: oneDay*(indexPath.row ) ];//initWithTimeIntervalSinceNow是从现在往前后推的秒数
            
        }else{
            
            theDate = nowDate;
        }
        // 星期label
        NSString *str = [self weekdayStringFromDate:theDate];
        UILabel *dayLabel = [UILabel labelWithText:str andTextColor:[UIColor lightGrayColor] andFontSize:13];
        [cell addSubview:dayLabel];
        [dayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(8);
            make.centerX.equalTo(cell);
        }];
        
        
//        cell.time = self.timeStr;
//        cell.DayLabel.text = @"11";
    }
    // 设置上午中午晚上
    if (indexPath.section == 1 && indexPath.row == 0) {
        UILabel *label = [UILabel labelWithText:@"上午" andTextColor:[UIColor colorWithHex:0x999999] andFontSize:12];
        label.numberOfLines = 2;
        [cell addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(cell);
            make.top.offset(4);
        }];
    }else if(indexPath.section == 2 && indexPath.row == 0)
    {
        UILabel *label = [UILabel labelWithText:@"下午" andTextColor:[UIColor colorWithHex:0x999999] andFontSize:12];
        label.numberOfLines = 2;
        [cell addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(cell);
            make.top.offset(4);
        }];
    }else if (indexPath.section == 3 && indexPath.row == 0)
    {
        UILabel *label = [UILabel labelWithText:@"晚上" andTextColor:[UIColor colorWithHex:0x999999] andFontSize:12];
        label.numberOfLines = 2;
        [cell addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(cell);
            make.top.offset(4);
        }];
        
    }

    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
// 设置layout
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    CGFloat width = (self.collectionview.width - 7) / 8;
    CGFloat height = (self.collectionview.height - 3) / 4;
    self.layout.itemSize = CGSizeMake(width, height);
    [self.layout setSectionInset:UIEdgeInsetsMake(1, 1, 0, 1)];
//    UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)

}

#pragma mark - <UICollectionViewDelegate>


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 根据点击获取被点击的cell
    CLBaseCollectionViewCell *cell = (CLBaseCollectionViewCell *)[self.collectionview cellForItemAtIndexPath:indexPath];

    // 判断这个cell里边是否有号
    if (indexPath.row > 0 && indexPath.section != 0 && cell.subviews.count > 1) {
            cell.backgroundColor = [UIColor lightGrayColor];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:.2 animations:^{
                cell.backgroundColor = [UIColor whiteColor];
                NSString *time = [self getNDay:indexPath.row];
                NSInteger cellNumber = indexPath.section;
                
#warning mark - 传递给下个页面的数据
                CLSubmitDataVC *submitDataVc = [[CLSubmitDataVC alloc]init];
                // 传递的疾病字典
                submitDataVc.diseaseStatus = self.sickDataDict;
                // 传递的时间
                submitDataVc.weakDay = time;
                
                [self.navigationController pushViewController:submitDataVc animated:YES];
                NSLog(@"-------------");
                NSLog(@"%@---------%zd",time,cellNumber - 1);

            }];
        });
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




#pragma mark - 获取系统时间,根据时间得出是星期几
// 获取时间
-(NSString *)getNDay:(NSInteger)n{
    
    NSDate*nowDate = [NSDate date];
    
    NSDate* theDate;
    
    if(n!=0){
        
        NSTimeInterval  oneDay = 24*60*60*1;  //1天的长度
        theDate = [nowDate initWithTimeIntervalSinceNow: oneDay*n ];//initWithTimeIntervalSinceNow是从现在往前后推的秒数
        
    }else{
        
        theDate = nowDate;
    }
    
    NSDateFormatter *date_formatter = [[NSDateFormatter alloc] init];
    [date_formatter setDateFormat:@"MM-dd"];
    NSString *the_date_str = [date_formatter stringFromDate:theDate];
    
    return the_date_str;
}
// 根据date返回星期几
- (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
    
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];
    
}


@end
